/**
 * 隐患上报 编辑页
 *
 * 作者 wanghw
 * 日期 2024-08-20 09:23:04
 */
layui.use(['jquery','febs', 'form', 'validate', 'fileUpload', 'fileUpload','autocomplete','commonJS','laydate'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        $view = $('#hazardReportMessageReply-edit'),
        commonJS = layui.commonJS,
        validate = layui.validate;

    var messageList = [];

    form.verify(validate);
    form.render();
    initFormValue();

    function initFormValue() {
        if(hazardReport){
            form.val("hazardReportMessageReply-edit-form", {
                "reportId": hazardReport.reportId,
                "tenantId": hazardReport.tenantId,
            });
            messageList = hazardReport.messageList;
            if(messageList.length>0){
                messageList.forEach(item =>{
                    let html = '<div class="layui-form-item">' +
                    '                        <div class="layui-row">';
                        if(item.userId == currentUser.userId){
                            html += '<div class="layui-col-md4" style="float: right">';
                        }else{
                            html += '<div class="layui-col-md5" style="float: left;margin-left: 5%">';
                        }
                    html += ' <a class="messageImg"><img style="width: 1.9rem;margin-right:.5rem;border-radius: 50%" src="'+ctx+'febs/images/avatar/'+item.avatar+'"></a>' +
                    '         <div class="layui-input-block layui-input-block-html"><b style="color: #00d57b">'+item.realName+'</b>  '+item.messageDate+'</div>' +
                    '         <div class="layui-input-block layui-input-block-html">'+item.message+'</div>' +
                    '       </div>' +
                    '     </div>' +
                    '  </div>';

                    $("#reportMessageBody").append(html);
                })
            }
        }
        form.render();
    }


    form.on('submit(hazardReportMessageReply-edit-form-submit)', function (data) {
        let message = data.field.message;
        if(message == '' || message == null){
            febs.alert.error("未填写留言内容");
            return false;
        }
        addOrUpdate(data.field);
        return false;
    });

    function addOrUpdate(data) {
        let uName = currentUser.uname;
        let currentTime = new Date().toLocaleString();
        febs.postArray(ctx + 'hazardReportMessage', data, function () {
            let html = '<div class="layui-form-item">' +
                '                        <div class="layui-row">'+
                '<div class="layui-col-md4" style="float: right">'+
                '                                <a class="messageImg"><img style="width: 1.9rem;margin-right:.5rem;border-radius: 50%" src="'+ctx+'febs/images/avatar/'+currentUser.avatar+'"></a>' +
                '                                <div class="layui-input-block layui-input-block-html"><b style="color: #00d57b"> '+uName+'</b>  '+currentTime+'</div>' +
                '                                <div class="layui-input-block layui-input-block-html">'+data.message+'</div>' +
                '                            </div>' +
                '                        </div>' +
                '                    </div>';
            $("#reportMessageBody").append(html);
            $("#message").val('');
        });
    }

    function getTenantId(){
        if(hazardReport){
            return hazardReport.tenantId;
        }else {
            return POPUP_DATA.tenantId;
        }
    }


});